package com.dzz.sjgl.utils;

import java.util.ArrayList;
import java.util.List;

import com.aliasi.sentences.IndoEuropeanSentenceModel;
import com.aliasi.sentences.SentenceModel;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;
import com.aliasi.tokenizer.Tokenizer;
import com.aliasi.tokenizer.TokenizerFactory;


/**
 * @ClassName SpliteTextInSentence
 * @Author: zhaozy
 * @Date: 2019/3/18 23:11
 * @Description:
 **/

public class SpliteTextInSentence {
    private static final TokenizerFactory TOKENIZER_FACTORY = IndoEuropeanTokenizerFactory.INSTANCE;
    private static final SentenceModel SENTENCE_MODEL = new IndoEuropeanSentenceModel();

  /*  public static void main(String[] args) {
//        String text = Word2String.getText("C:\\Workspace\\p\\sjgl\\src\\main\\resources\\word\\2016年全国高考英语试题及答案(全国-II卷)精校版.doc");
        String text = "Read this book l View details l Add to Collection Children with School problme:A Physkian' s Manual The children paediatric Society; Andrews Debra;Mahoney WilliamJ,2012 I wiley Available The physician' s guide to diagnosing and treating learning disabilities in children.1 to 10 Canadians have a learning disability,and doctors must be able to idcntify,diagnose,trear,and manage children… Read this book I view details I Add to Collection Songs in Their Heads:Music and Its Meaning In Children' s Lives Campbell Patricia Shehan,1989 I Oxford University Press Available This book explores the intrest and needs of children in their expressed thounts and actual \" musicking\" behaviours, This text examines the songs they sing,the ryhthms… Read this book I view details I Add to Collection Young Children as Artists:Art and Design in the Earty Years and Kay Stage 1 Tutchell Suzy 2014 I Taylor Francis Available From the monment a child is bom,they intctract with the world,looking at colours,feeding texrures;constructing mental and physical images of what they see and experience.Within all early years… Read this book I view details I Add to Collection Big Ideas for Littles Kids:TEAching Philosophy Through Children' s Liferature Wartnberg Thomas E.2014 I Rowman&Littlefield Publishers Available Big Ideas for Little Kids includes everything a teacher,or a college student needs to teach philosophy to elementary school children from picture books.Written in a clear and accessible style… Read this book I view details I Add to Collection 46.Suppose you are doing research on children' s relationship skills,you may want to read . ";
        List<String> sl = testChunkSentences(splitfuhao(text));
        if (sl.isEmpty()) {
            System.out.println("没有识别到句子");
        }
        for (String row : sl) {
            row = row.replaceAll("\\s{1,}", " ")
//                    .replaceAll("[A-G][.]",".")
//                    .replaceAll("[0-9]{1,2}[.]","")
            ;
            String regEx = "[\\u4e00-\\u9fa5]";
            String term = row.replaceAll(regEx, "aa");
            int count = term.length() - row.length();
            if (count > 3) {
                continue;
            }
            if (row.length() > 10) {
                System.out.println(row + "===" + row.length());
            }
        }
    }*/

    public static List<String> testChunkSentences(String text) {
        List<String> result = new ArrayList<>();
        List<String> tokenList = new ArrayList<>();
        List<String> whiteList = new ArrayList<>();
        Tokenizer tokenizer = TOKENIZER_FACTORY.tokenizer(text.toCharArray(),
                0, text.length());
        tokenizer.tokenize(tokenList, whiteList);
        String[] tokens = new String[tokenList.size()];
        String[] whites = new String[whiteList.size()];
        tokenList.toArray(tokens);
        whiteList.toArray(whites);
        int[] sentenceBoundaries = SENTENCE_MODEL.boundaryIndices(tokens,
                whites);
        int sentStartTok = 0;
        int sentEndTok;

        for (int sentenceBoundary : sentenceBoundaries) {
            StringBuilder sb = new StringBuilder();
            sentEndTok = sentenceBoundary;
            for (int j = sentStartTok; j <= sentEndTok; j++) {
                sb.append(tokens[j]).append(whites[j + 1]);
            }
            sentStartTok = sentEndTok + 1;
            result.add(sb.toString());
        }
        //System.out.println("Final result:" + result);
        return result;
    }

    //替换中文标点符号，用于检测是否识别中文分句
    public static String splitfuhao(String str) {
        String[] chineseInterpunction = {"“", "”", "‘", "’", "。", "，", "；", "：", "？", "！", "……", "—", "～", "（", "）", "《", "》"};
        String[] englishInterpunction = {"\"", "\"", "'", "'", ".", ",", ";", ":", "?", "!", "…", "-", "~", "(", ")", "<", ">"};
        for (int j = 0; j < chineseInterpunction.length; j++) {
            //alert("txt.replace("+chineseInterpunction[j]+", "+englishInterpunction[j]+")");
            //String reg=str.matches(chineseInterpunction[j],"g");
            str = str.replace(chineseInterpunction[j], englishInterpunction[j] + " ");
        }
        return str;
    }
}
